/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.legendshop.dao.support.PageSupport;
import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.constants.SysParamGroupEnum;
import com.legendshop.oa.model.SystemParameter;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.pages.RedirectPage;
import com.legendshop.oa.service.SystemParameterService;
import com.legendshop.oa.tag.ResourceBundleHelper;
import com.legendshop.oa.util.PageSupportHelper;
import com.legendshop.oa.util.PropertiesUtil;

/**
 * 系统参数控制器.
 */
@Controller
@RequestMapping("/admin/system/systemParameter")
public class SystemParameterController extends BaseController {

	/** The system parameter service. */
	@Autowired
	private SystemParameterService systemParameterService;

	/**
	 * 查询系统参数列表.
	 * 
	 * @param request
	 * @param response
	 * @param curPageNO
	 * @param systemParameter
	 * @param groupId
	 * @return the string
	 */
	@RequestMapping("/query/{groupId}")
	public String query(HttpServletRequest request, HttpServletResponse response, String curPageNO, SystemParameter systemParameter,
			@PathVariable String groupId) {
		PageSupport<SystemParameter> ps = systemParameterService.getSystemParameterListPage(curPageNO, systemParameter, groupId);
		PageSupportHelper.savePage(request, ps);
		request.setAttribute("groupId", groupId);
		request.setAttribute("bean", systemParameter);

		return PathResolver.getPath(request, response, AdminTilesPage.PARAM_LIST_PAGE);
	}

	/**
	 * 保存系统参数.
	 * 
	 * @param request
	 * @param response
	 * @param systemParameter
	 * @return the string
	 */
	@RequestMapping(value = "/save")
	public String save(HttpServletRequest request, HttpServletResponse response, SystemParameter systemParameter) {
		SystemParameter parameter = systemParameterService.getSystemParameter(systemParameter.getName());
		String groupId = SysParamGroupEnum.SYS.name();
		if (parameter != null) {
			groupId = parameter.getGroupId();
			parameter.setValue(systemParameter.getValue());
			PropertiesUtil.setParameter(parameter);
			systemParameterService.update(parameter);
			saveMessage(request, ResourceBundleHelper.getSucessfulString());
		} else {
			saveMessage(request, ResourceBundleHelper.getErrorString());
		}
		return RedirectPage.PARAM_LIST_QUERY.getValue(request, response) + "/" + SysParamGroupEnum.getValue(groupId);
	}

	/**
	 * 删除系统参数.
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return the string
	 */
	@RequestMapping(value = "/delete/{id}")
	public String delete(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) {
		// systemParameterService.delete(id);
		saveMessage(request, ResourceBundleHelper.getErrorString());
		return PathResolver.getPath(request, response, RedirectPage.PARAM_LIST_QUERY);
	}

	/**
	 * 查看系统参数详情.
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return the string
	 */
	@RequestMapping(value = "/load/{id}")
	public String load(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) {
		SystemParameter systemParameter = systemParameterService.getSystemParameter(id);
		request.setAttribute("bean", systemParameter);
		return PathResolver.getPath(request, response, AdminTilesPage.PARAM_EDIT_PAGE);
	}

	/**
	 * 更新系统参数.
	 * 
	 * @param request
	 * @param response
	 * @param systemParameter
	 * @return the string
	 */
	@RequestMapping(value = "/update")
	public String update(HttpServletRequest request, HttpServletResponse response, @PathVariable SystemParameter systemParameter) {
		systemParameterService.update(systemParameter);
		return PathResolver.getPath(request, response, RedirectPage.PARAM_LIST_QUERY);
	}

}
